var express = require('express');
var router = express.Router();
var connection = require('../db/sql.js')
var user = require('../db/UserSql.js')
var jwt_decode = require('jwt-decode');
/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});



//验证码
let code = '';
//接入短信的sdk
var QcloudSms = require("qcloudsms_js");
//引入支付宝沙箱配置
const alipaySdk = require('../db/alipay.js')
//引入alibapay form
const AlipayFormData = require('alipay-sdk/lib/form').default
//注册手机号验证
//发送验证码
router.post('/api/code', function(req, res, next) {
	//前端给后端的数据
	let params = {
		userName: req.body.userName
	};
	// 短信应用 SDK AppID
	var appid = 1400187558; // SDK AppID 以1400开头
	// 短信应用 SDK AppKey
	var appkey = "dc9dc3391896235ddc2325685047edc7";
	// 需要发送短信的手机号码
	var phoneNumbers = [params.userName];
	// 短信模板 ID，需要在短信控制台中申请
	var templateId = 298000; // NOTE: 这里的模板ID`7839`只是示例，真实的模板 ID 需要在短信控制台中申请
	// 签名
	var smsSign = "生态验证码"; // NOTE: 签名参数使用的是`签名内容`，而不是`签名ID`。这里的签名"腾讯云"只是示例，真实的签名需要在短信控制台申请
	// 实例化 QcloudSms
	var qcloudsms = QcloudSms(appid, appkey);
	// 设置请求回调处理, 这里只是演示，用户需要自定义相应处理回调
	function callback(err, ress, resData) {
		if (err) {
			console.log("err: ", err);
		} else {
			code = ress.req.body.params[0];
			console.log(code, "验证码");
			res.send({
				data: {
					success: true,
					code: code
				}
			})
		}
	}
	var ssender = qcloudsms.SmsSingleSender();
	var paramss = [Math.floor(Math.random() * (9999 - 1000)) + 1000]; //发送的验证码
	ssender.sendWithParam("86", phoneNumbers[0], templateId,
		paramss, smsSign, "", "", callback);

})


//支付接口
// router.post('/api/paymant', function(req,res,next)(
// //接收前端给后端的订单号
// let orderId=req.req.body.orderId;
// //总价
// let price =req.body.price;
// //商品名称
// let  list =req.body.list.join('')
//  const formData = new AlipayFormData()
//     formData.setMethod('get')
//     formData.addField('returnUrl', 'http://www.baidu.com');//支付成功的回调
//     formData.addField('bizContent', {
//         outTradeNo: 'orderId', //订单号
//         productCode: 'FAST_INSTANT_TRADE_PAY', //产品码(写死的)
//         totalAmount: 'price',//金额
//         subject: list, //名称
//         // body: '小满的知识',//描述
// 		})

// 		const result =  alipaySdk.exec(
// 		  'alipay.trade.page.pay',
// 		  {},
// 		  { formData: formData },
// 		);

// 		    res.json({
// 				code:200,
// 				success:true,
// 		        paymentUrl:reult
// 		    })
// })



//修改订单状态
router.post('/api/submitOrder', function(req, res, next) {
	let token = req.headers.token;
	let phone = jwt_decode(token);
	//订单号
	let orderId = req.body.orderId;
	//购物车中选中的商品
	let shopArr = req.body.shopArr;
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		//当前用户id
		let userId = results[0].id;
		connection.query(`select * from store_order where uId = ${userId} and order_id = ${orderId}`,
			function(err, result) {
				//订单的id
				let id = result[0].id;

				connection.query(
					`update store_order set order_status = replace(order_status,'1','2') where id = ${id}`,
					function() {
						shopArr.forEach(v => {
							connection.query(`delete from goods_cart where id = ${v}`,
								function() {

								})
						})
						res.send({
							data: {
								code: 200,
								success: true
							}
						})
					})
			})
	})
})

//生成订单
router.post('/api/addOrder', function(req, res, next) {

	let token = req.headers.token;
	let phone = jwt_decode(token);
	//前端给后端传递的数据
	let goodsArr = req.body.arr;
	//生成订单号
	function setTimeDateFmt(s) {
		return s < 10 ? '0' + s : s;
	}

	function orderNumber() {
		const now = new Date();
		let fullYear = now.getFullYear();
		let month = setTimeDateFmt(now.getMonth() + 1);
		let day = setTimeDateFmt(now.getDate());
		let hour = setTimeDateFmt(now.getHours());
		let minutes = setTimeDateFmt(now.getMinutes());
		let seconds = setTimeDateFmt(now.getSeconds());
		let orderCode = fullYear + month + day + hour + minutes + seconds + (Math.round(Math.random() *
			1000000));
		return orderCode;
	}
	//商品名称
	let goodsName = [];
	//订单总价
	let goodsPrice = 0;
	//订单商品总数量
	let goodsNum = 0;
	//订单号
	let orderId = orderNumber();
	console.log(orderId);
	goodsArr.forEach(v => {
		goodsName.push(v.name);
		goodsNum += parseInt(v.num);
		goodsPrice += v.num * v.pprice;
	})

	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		console.log(results, "结果");
		//当前用户id
		let userId = results[0].id;
		console.log(userId, "用户ID");
		console.log(userId, goodsName);
		connection.query(
			`insert into store_order (uId,order_id,goods_name,goods_price,goods_num,order_status) values ('${userId}','${orderId}','${goodsName}','${goodsPrice}','${goodsNum}','1')`,
			function() {
				connection.query(
					`select * from store_order where uId = ${userId} and order_id = ${orderId}`,
					function(err, result) {
						res.send({
							data: {
								code: 200,
								success: true,
								data: result
							}
						})
					})
			})
	})
})

//删除购物车商品数据
router.post('/api/deleteCart', function(req, res, next) {
	let goodsId = req.body.goodsId;
	for (var i = 0; i < goodsId.length; i++) {
		connection.query(`delete from goods_cart where id=${goodsId[i]}`, function(e, r) {
			res.json({
				data: {
					success: true
				}
			})
		})
	}
})

//加入购物车
router.post('/api/addCart', function(req, res, next) {
	let token = req.headers.token;
	let phone = jwt_decode(token);
	//商品id
	let goods_id = req.body.goods_id;
	//用户输入的商品数量
	let num = req.body.num;
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		//当前用户id
		let userId = results[0].id;
		connection.query(`select * from goods_search where id = ${goods_id}`, function(err, result) {
			let name = result[0].name;
			let imgUrl = result[0].imgUrl;
			let pprice = result[0].pprice;
			//查询当前用户之前是否添加过这个商品
			connection.query(
				`select * from goods_cart where uId = ${userId} and goods_id = ${goods_id}`,
				function(err, data) {
					if (data.length > 0) {
						//如果当前用户已经添加过本商品,就让数量增加
						connection.query(
							`update goods_cart set num = replace(num,${data[0].num},${ parseInt(num) + parseInt(data[0].num) }) where id = ${data[0].id}`,
							function(e, r) {
								res.json({
									data: {
										success: "加入成功"
									}
								})
							})
					} else {
						//如果当前用户之前没有加入过本商品,需要添加进入
						connection.query(
							'insert into goods_cart (uId,goods_id,name,imgUrl,pprice,num) values ("' +
							userId + '","' + goods_id + '","' + name + '","' + imgUrl +
							'","' + pprice + '","' + num + '")',
							function(err, data) {
								res.json({
									data: {
										success: "加入成功"
									}
								})
							})
					}
				})
		})
	})
})


//1. 当前用户
//2. 当前用户--->哪一个商品的数量发展变化  [查询]   原来的数量
//3. 替换 ===> 把前端给的值拿过来, 把原来数量替换掉

//修改当前用户购物车商品数量
router.post('/api/updateNumCart', function(req, res, next) {
	let token = req.headers.token;
	let phone = jwt_decode(token);
	//商品id
	console.log(req.body);
	let goodsId = req.body.goodsId;
	//用户输入的商品数量
	let num = req.body.num;
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		//当前用户id
		let userId = results[0].id;
		connection.query(`select * from goods_cart where uId = ${userId} and goods_id = ${goodsId}`,
			function(err, result) {
				//数据中当前的数量
				let goods_num = result[0].num;
				//当前的id号
				let id = result[0].id;
				//修改[替换]
				connection.query(
					`update goods_cart set num = replace(num,${goods_num},${num}) where id = ${id}`,
					function(e, r) {
						res.json({
							data: {
								success: true
							}
						})
					})
			})

	})
})


//获取当前用户购物车列表
router.post('/api/selectCart', function(req, res, next) {
	let token = req.headers.token;
	let phone = jwt_decode(token);
	console.log(phone);
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		//当前用户id
		let userId = results[0].id;
		connection.query(`select * from goods_cart where uId = ${userId}`, function(err, result) {
			res.json({
				data: result
			})
		})
	})
})


//当前用户修改收货地址
router.post('/api/updateAddress', function(req, res, next) {
	let token = req.headers.token;
	let phone = jwt_decode(token);
	let name = req.body.name;
	let tel = req.body.tel;
	let province = req.body.province;
	let city = req.body.city;
	let district = req.body.district;
	let address = req.body.address;
	let isDefault = req.body.isDefault;
	let id = req.body.id;

	//获取userId
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		let userId = results[0].id;
		connection.query(`select * from address where userId = ${userId} and isDefault = ${isDefault}`,
			function(err, result) {
				let childId = result[0].id;
				connection.query(
					`update address set isDefault = replace(isDefault,"1","0") where id = ${childId}`,
					function(e, r) {
						let updateSql =
							`update address set name = ?,tel = ?,province = ?,city = ?,district = ?,address = ?,isDefault = ?,userId = ? where id = ${id}`
						connection.query(updateSql, [name, tel, province, city, district,
							address, isDefault, userId
						], function(err, result) {
							res.send({
								data: {
									success: '成功'
								}
							})
						})
					})
			})
	})
})

//当前用户新增收货地址
router.post('/api/addAddress', function(req, res, next) {

	let token = req.headers.token;
	let phone = jwt_decode(token);
	let name = req.body.name;
	let tel = req.body.tel;
	let province = req.body.province;
	let city = req.body.city;
	let district = req.body.district;
	let address = req.body.address;
	let isDefault = req.body.isDefault;

	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		let id = results[0].id;
		connection.query(
			`update address set isDefault = replace(isDefault,"1","0") where userId = ${id}`,
			function(e, r) {
				let sqlInert =
					'insert into address (name,tel,province,city,district,address,isDefault,userId) values ("' +
					name + '","' + tel + '","' + province + '","' + city + '","' + district +
					'","' + address + '","' + isDefault + '","' + id + '")';
				connection.query(sqlInert, function(err, result, field) {
					res.send({
						data: {
							success: "成功"
						}
					})
				})
			})
	})
})



//当前用户查询收货地址
router.post('/api/selectAddress', function(req, res, next) {
	let token = req.headers.token;
	console.log(token);
	let phone = jwt_decode(token);
	console.log(phone, "phone");
	connection.query(`select * from user where phone = "${phone.phone}"`, function(error, results, fields) {
		console.log(results, "返回数组");
		let id = results[0].id;
		console.log(id, "查询的id");
		connection.query(`select * from address where userId = ${id}`, function(err, result, field) {
			console.log(result, "返回地址");
			res.send({
				data: result
			})
		})
	})
})


//第三方登录
router.post('/api/loginother', function(req, res, next) {
	//前端给后端的数据
	let params = {
		provider: req.body.provider, //登录方式
		openid: req.body.openid, //用户身份id
		nickName: req.body.nickName, //用户昵称
		avatarUrl: req.body.avatarUrl //用户头像
	};

	//查询数据库中有没有此用户
	connection.query(user.queryUserName(params), function(error, results, fields) {
		if (results.length > 0) {
			//数据库中存在      : 读取
			connection.query(user.queryUserName(params), function(e, r) {
				res.send({
					data: r[0]
				})
			})
		} else {
			//数据库中[不]存在  : 存储 ==>读取
			connection.query(user.insertData(params), function(er, result) {
				connection.query(user.queryUserName(params), function(e, r) {
					res.send({
						data: r[0]
					})
				})
			})
		}
	})

})

//注册===>增加一条数据
router.post('/api/addUser', function(req, res, next) {
	//前端给后端的数据
	let params = {
		userPhone: req.body.userName,
		userCode: req.body.code
	};
	if (params.userCode == code) {
		connection.query(user.insertData(params), function(error, results, fields) {
			connection.query(user.queryUserName(params), function(err, result) {
				res.send({
					data: {
						success: true,
						msg: "注册成功",
						data: result[0]
					}
				})
			})
		})
	}

})



//注册验证手机号是否存在
router.post('/api/registered', function(req, res, next) {

	//前端给后端的数据
	let params = {
		userPhone: req.body.phone
	};
	//查询手机号是否存在
	connection.query(user.queryUserName(params), function(error, results, fields) {
		if (results.length > 0) {
			res.send({
				data: {
					success: false,
					msg: "用户已经存在"
				}
			})
		} else {
			res.send({
				data: {
					success: true
				}
			})
		}
	})

})




//用户登录
router.post('/api/login', function(req, res, next) {

	//前端给后端的数据
	let params = {
		userPhone: req.body.userPhone,
		userPwd: req.body.userPwd
	}
	//查询用户名或者手机号存在不存在
	console.log(params.userPhone);
	connection.query(user.queryUserName(params), function(error, results, fields) {
		console.log(results, "用户");
		if (results.length > 0) {
			connection.query(user.queryUserPwd(params), function(err, result) {
				if (result.length > 0) {
					res.send({
						data: {
							success: true,
							msg: "登录成功",
							data: result[0]
						}
					})
				} else {
					res.send({
						data: {
							success: false,
							msg: "密码不正确"
						}
					})
				}
			})
		} else {
			res.send({
				data: {
					success: false,
					msg: "用户不存在"
				}
			})
		}
	})
});


//根据id进入商品详情
router.get('/api/goods/id', function(req, res, next) {
	let id = req.query.id;
	connection.query("select * from goods_search where id=" + id + "", function(error, results, fields) {
		if (error) throw error;
		res.send({
			code: '0',
			data: results
		})

	})
})




//商品分类
router.get('/api/goods/list', function(req, res, next) {
	res.json({
		code: 0,
		data: [{
				id: 1,
				name: "水果",
				data: [{
						name: "春季",
						list: [{
								id: 1,
								name: "草莓",
								imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102507261.jpg.middle.jpg"
							},
							{
								id: 2,
								name: "菠萝",
								imgUrl: "https://www.nongsc.cn/file/upload/202001/07/140028661.png.middle.png"
							}
						]
					},
					{
						name: "夏季",
						list: [{
								id: 1,
								name: "西瓜",
								imgUrl: "https://www.nongsc.cn/file/upload/202007/18/154958511.png.middle.png"
							},
							{
								id: 2,
								name: "芒果",
								imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102556511.jpg.middle.jpg"
							}
						]
					}
				]
			},
			{
				id: 2,
				name: "生鲜",
				data: [{
						name: "牛肉",
						list: [{
								id: 1,
								name: "牛肉",
								imgUrl: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.ntimg.cn%2F20130924%2F10894569_152616434125_2.jpg&refer=http%3A%2F%2Fpic.ntimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664531545&t=276fd7c0fd35a091522e544be641f126"
							}
						]
					},
					{
						name: "猪肉",
						list: [{
								id: 1,
								name: "猪肉",
								imgUrl: "https://img0.baidu.com/it/u=234521220,1503671377&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500"
							}
						]
					},
					{
						name: "鸡肉",
						list: [{
								id: 1,
								name: "整鸡",
								imgUrl: "https://www.nongsc.cn/file/upload/202010/26/105734291.jpg.middle.jpg"
							}
						]
					},
					{
						name: "鱼肉",
						list: [{
								id: 1,
								name: "鱼肉",
								imgUrl: "https://img0.baidu.com/it/u=3884950055,1380986086&fm=253&fmt=auto&app=138&f=JPEG?w=759&h=500"
							}
						]
					}
				]
			},

			{
				id: 3,
				name: "蔬菜",
				data: [{
						// name: "裙装",
						list: [{
								id: 1,
								name: "白菜",
								imgUrl: "https://img0.baidu.com/it/u=4234149454,3553848865&fm=253&fmt=auto&app=138&f=JPEG?w=310&h=195"
							},
							{
								id: 2,
								name: "菜心",
								imgUrl: "https://img2.baidu.com/it/u=4090897099,4007297242&fm=253&fmt=auto&app=138&f=JPEG?w=425&h=373"
							}
						]
					}
				]
			},

		]
	})
});


//首次第一次触底的数据
router.get('/api/index_list/1/data/2', function(req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*'),
		res.json({
			code: "0",
			data: [{
				type: "commodityList",
				data: [{
						id: 1,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
						name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
						pprice: "70",
						oprice: "59.64",
						discount: "5.2"
					},
					{
						id: 2,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
						name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 3,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
						name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 4,
						imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
						name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					}
				]
			}]
		})
})
//运动户外第二次触底的数据
router.get('/api/index_list/2/data/3', function(req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*'),
		res.json({
			code: "0",
			data: [{
				type: "commodityList",
				data: [{
						id: 1,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
						name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
						pprice: "70",
						oprice: "59.64",
						discount: "5.2"
					},
					{
						id: 2,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
						name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 3,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
						name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 4,
						imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
						name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					}
				]
			}]
		})
})
//运动户外第一次触底的数据
router.get('/api/index_list/2/data/2', function(req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*'),
		res.json({
			code: "0",
			data: [{
				type: "commodityList",
				data: [{
						id: 1,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
						name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
						pprice: "70",
						oprice: "59.64",
						discount: "5.2"
					},
					{
						id: 2,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
						name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 3,
						imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
						name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					},
					{
						id: 4,
						imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
						name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
						pprice: "299",
						oprice: "659",
						discount: "5.2"
					}
				]
			}]
		})
})

router.get('/api/index_list/2/data/1', function(req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*'),
		res.json({
			code: "0",
			data: [{
					type: "bannerList",
					imgUrl: "http://pmlafc3d8.pic32.websiteonline.cn/upload/b2_meitu_1.jpg"
				},
				{
					type: "iconsList",
					data: [{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "新"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "鲜"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "蔬"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "果"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "绿"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "色"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "健"
						},
						{
							imgUrl: "../../static/img/新鲜果蔬_新鲜蔬菜包菜.png",
							name: "康"
						}
					]
				},
				{
					type: "hotList",
					data: [{
							id: 1,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102556251.jpg.middle.jpg",
							name: "产地货源 广西贵妃芒9斤整箱彩虹芒果 贵妃广西水果批发非金煌芒",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 2,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140747221.jpg.middle.jpg",
							name: "新鲜带壳小竹笋2022年湖南高山春笋野生水竹笋现扯现发带壳山竹笋",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 3,
							imgUrl: "https://www.nongsc.cn/file/upload/202109/02/160528611.jpg.middle.jpg",
							name: "自有基地散养害黑猪前腿肉 冷鲜黑猪肉 国家地理标志产品",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						}
					]
				},
				{
					type: "shopList",
					data: [{
							bigUrl: "../../static/img/shop.jpg",
							data: [{
									id: 1,
									imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102659281.jpg.middle.jpg",
									name: "厂家直销优良苹果苗 松脆味甜可口 当年结果苹果苗",
									pprice: "299",
									oprice: "659",
									discount: "5.2"
								},
								{
									id: 2,
									imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102546211.jpg.middle.jpg",
									name: "基地现货直供3年结果嫁接枣树苗 易成活高产量枣树苗 品种齐全",
									pprice: "299",
									oprice: "659",
									discount: "5.2"
								},
								{
									id: 3,
									imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102824331.jpg.middle.jpg",
									name: "嫁接红心猕猴桃苗批发 软枣猕猴桃树苗基地 供应徐香猕猴桃苗价格8",
									pprice: "299",
									oprice: "659",
									discount: "5.2"
								},
								{
									id: 2,
									imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102546211.jpg.middle.jpg",
									name: "基地现货直供3年结果嫁接枣树苗 易成活高产量枣树苗 品种齐全",
									pprice: "299",
									oprice: "659",
									discount: "5.2"
								},
								{
									id: 3,
									imgUrl: "https://www.nongsc.cn/file/upload/202207/06/102824331.jpg.middle.jpg",
									name: "嫁接红心猕猴桃苗批发 软枣猕猴桃树苗基地 供应徐香猕猴桃苗价格8",
									pprice: "299",
									oprice: "659",
									discount: "5.2"
								},
							]
						},
						{
							bigUrl: "../../static/img/shop.jpg",
		data: [{
				id: 1,
				imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
				name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
				pprice: "70",
				oprice: "59.64",
				discount: "5.2"
			},
			{
				id: 2,
				imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
				name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
				pprice: "299",
				oprice: "659",
				discount: "5.2"
			},
			{
				id: 3,
				imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
				name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
				pprice: "299",
				oprice: "659",
				discount: "5.2"
			},
			{
				id: 4,
				imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
				name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
				pprice: "299",
				oprice: "659",
				discount: "5.2"
			},
			{
				id: 5,
				imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
				name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
				pprice: "299",
				oprice: "659",
				discount: "5.2"
			},
			{
				id: 6,
				imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
				name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
				pprice: "299",
				oprice: "659",
				discount: "5.2"
			},
		]
						}
					]
				},
				{
					type: "commodityList",
					data: [{
							id: 1,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
							name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
							pprice: "70",
							oprice: "59.64",
							discount: "5.2"
						},
						{
							id: 2,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
							name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 3,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
							name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 4,
							imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
							name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 5,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
							name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
						{
							id: 6,
							imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
							name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
							pprice: "299",
							oprice: "659",
							discount: "5.2"
						},
					]
				}
			]
		})
});



router.get('/api/index_list/data', function(req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*'),
		res.send({
			"code": 0,
			"data": {
				topBar: [{
						id: 1,
						name: '推荐',

					},
					{
						id: 2,
						name: '生态农业'
					},
					{
						id: 3,
						name: '生鲜食品'
					},

					{
						id: 4,
						name: '粮油蛋奶'
					},

					{
						id: 5,
						name: '进口食品'

					},

					{
						id: 6,
						name: '田园花夰'
					},
				
				],
				data: [{
						type: "swiperList",
						data: [{
								imgUrl: '../../static/img/swiper1.jpg'
							},
							{
								imgUrl: '../../static/img/swiper2.jpg'
							},
							{
								imgUrl: '../../static/img/swiper3.jpg'
							}
						]
					},
					{
						type: 'recommendList',
						data: [{
								bigUrl: '../../static/img/oioo.jpg',
								data: [{
										imgUrl: '../../static/img/Children1.jpg'
									},
									{
										imgUrl: '../../static/img/Children2.jpg'
									},
									{
										imgUrl: '../../static/img/Children3.jpg'
									}
								]
							},
							{
								bigUrl: '../../static/img/Furnishing.jpg',
								data: [{
										imgUrl: '../../static/img/Furnishing1.jpg'
									},
									{
										imgUrl: '../../static/img/Furnishing2.jpg'
									},
									{
										imgUrl: '../../static/img/Furnishing3.jpg'
									}
								]
							}
						]
					},
					{
						type: 'commodityList',
						data: [{
								id: 1,
								imgUrl: "https://www.nongsc.cn/file/upload/202207/07/140842351.jpg.middle.jpg",
								name: "云南高山麻叶青白菜长白菜津绿白菜青菜时令蔬菜新鲜现摘",
								pprice: "70",
								oprice: "59.64",
								discount: "5.2"
							},
							{
								id: 2,
								imgUrl: "https://www.nongsc.cn/file/upload/202207/07/102425861.jpg.middle.jpg",
								name: "产地直发广西金都一号红心火龙果小果5斤京都火龙果时令水果新鲜",
								pprice: "299",
								oprice: "659",
								discount: "5.2"
							},
							{
								id: 3,
								imgUrl: "https://www.nongsc.cn/file/upload/202207/06/103029251.jpg.middle.jpg",
								name: "组培H5珠宝蓝莓树苗基地 薄雾蓝莓苗价格 绿宝石法新蓝莓苗批发",
								pprice: "299",
								oprice: "659",
								discount: "5.2"
							},
							{
								id: 4,
								imgUrl: "https://www.nongsc.cn/file/upload/2022/0506/182/183458671.jpg.middle.jpg",
								name: "东北鹿茸新鲜鹿茸人参枸杞鹿茸泡酒",
								pprice: "299",
								oprice: "659",
								discount: "5.2"
							}
						]
					}
				]
			}
		})
});


//查询数据库
router.get("/api/goods/search", function(req, res, next) {
	//desc降序     asc升序
	//获取对象的key
	let [goodsName, orderName] = Object.keys(req.query);
	//name参数的值
	let name = req.query.name;
	//orderName的key的值
	let order = req.query[orderName];

	connection.query("select * from goods_search where name like '%" + name + "%' order by " + orderName + " " +
		order + "",
		function(error, results, fields) {
			if (error) throw error;
			res.send({
				code: "0",
				data: results
			})
		});

});

module.exports = router;
